package buss.console.util;

//所需导入的jar包在下侧下载
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import org.apache.log4j.Logger;

import com.base.pojo.topchannel.Region;

public class PinYinUtil {
	private static Logger log = Logger.getLogger(PinYinUtil.class);
	
	// ------------------------将汉字转换为全拼-------------------------------
	public static String getPingYin(String src) {
		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符
				if (java.lang.Character.toString(t1[i]).matches(
						"[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				} else {
					t4 += java.lang.Character.toString(t1[i]);
				}
			}

			// log.info(t4);
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			log.error(e1);
		}
		return t4;
	}

	// ----------------------------------------返回中文的首字母--------------------
	public static String getPinYinHeadChar(String str) {
		String convert = "";
		for (int j = 0; j < str.length(); j++) {
			char word = str.charAt(j);
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null) {
				convert += pinyinArray[0].charAt(0);
			} else {
				convert += word;
			}
		}
		return convert;
	}
	// ----------------------------------------返回中文的首字的首字母--------------------
	public static char getPinYinFristChar(String str) {
		char FristChar='a';
		if('长'==str.charAt(0)||'重'==str.charAt(0)||'厦'==str.charAt(0)){
			FristChar = PinyinHelper.toHanyuPinyinStringArray(str.charAt(0))[1].charAt(0);
		}else{
		 FristChar = PinyinHelper.toHanyuPinyinStringArray(str.charAt(0))[0].charAt(0);
		}
		return FristChar;
	}
	// -------------------------将字符串转移为ASCII码-----------------------------
	public static String getCnASCII(String cnStr) {
		StringBuffer strBuf = new StringBuffer();
		byte[] bGBK = cnStr.getBytes();
		for (int i = 0; i < bGBK.length; i++) {
			// log.info(Integer.toHexString(bGBK[i]&0xff));
			strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
		}
		return strBuf.toString();
	}
	//---------------------------将城市列表分别存到对应首字母list-----------------------
	public static Map<String,List<Region>> getCityList(List<Region> list){
		log.info("----------StratTime :  "+System.currentTimeMillis());
		Map<String,List<Region>> map = new TreeMap<String, List<Region>>();
//		for (char i = 'a'; i <= 'z'; i++) {
//			map.put(String.valueOf(i), new ArrayList<Region>());
//		}
		for(Region region :list){
//			map.get(String.valueOf(getPinYinFristChar(region.getRegionName()))).add(region);
			String firstchar = String.valueOf(
					Character.toUpperCase(getPinYinFristChar(
							region.getRegionName())
							)
					);
			List<Region> cList = map.get(firstchar);
			if (cList == null) {
				cList = new ArrayList<Region>();
				map.put(firstchar, cList);
			}
			cList.add(region);
		};
		log.info("-----------------------------------------endTime :  "+System.currentTimeMillis());
		
		return map;
	}
	// --------------------------测试拼音转汉字是否正确----------------------------
	public static void main(String[] args) {
		log.info(getPingYin("济源市"));
		// 返回拼音的全拼样式
		log.info(getPinYinHeadChar("西安市"));
		// 返回拼音的首字母
		log.info(getCnASCII("西安市"));
		// 返回拼音的ASCII
		log.info("北京".charAt(0));
		char a = "北京".charAt(0);  
		log.info(PinyinHelper.toHanyuPinyinStringArray("北京".charAt(0))[0].charAt(0));
		log.info((char)(a+49));
	}
}
